package cn.iocoder.yudao.module.erp.service.invoicechecklist;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklist.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoicechecklist.InvoiceCheckListDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.invoicechecklist.InvoiceCheckListMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * ERP 采购发票审核单 Service 实现类
 *
 * @author 源码
 */
@Service
@Validated
public class InvoiceCheckListServiceImpl implements InvoiceCheckListService {

    @Resource
    private InvoiceCheckListMapper invoiceCheckListMapper;

    @Override
    public Long createInvoiceCheckList(InvoiceCheckListSaveReqVO createReqVO) {
        // 插入
        InvoiceCheckListDO invoiceCheckList = BeanUtils.toBean(createReqVO, InvoiceCheckListDO.class);
        invoiceCheckListMapper.insert(invoiceCheckList);
        // 返回
        return invoiceCheckList.getId();
    }

    @Override
    public void updateInvoiceCheckList(InvoiceCheckListSaveReqVO updateReqVO) {
        // 校验存在
        validateInvoiceCheckListExists(updateReqVO.getId());
        // 更新
        InvoiceCheckListDO updateObj = BeanUtils.toBean(updateReqVO, InvoiceCheckListDO.class);
        invoiceCheckListMapper.updateById(updateObj);
    }

    @Override
    public void deleteInvoiceCheckList(Long id) {
        // 校验存在
        validateInvoiceCheckListExists(id);
        // 删除
        invoiceCheckListMapper.deleteById(id);
    }

    private void validateInvoiceCheckListExists(Long id) {
        if (invoiceCheckListMapper.selectById(id) == null) {
            throw exception(INVOICE_CHECK_LIST_NOT_EXISTS);
        }
    }

    @Override
    public InvoiceCheckListDO getInvoiceCheckList(Long id) {
        return invoiceCheckListMapper.selectById(id);
    }

    @Override
    public PageResult<InvoiceCheckListDO> getInvoiceCheckListPage(InvoiceCheckListPageReqVO pageReqVO) {
        return invoiceCheckListMapper.selectPage(pageReqVO);
    }

//    根据发票审核单编号 修改发票审核单状态
    @Override
    public void updateStatusByCheckInvoiceId(String checkInvoiceId, String invoiceStatus) {
        //    根据发票审核单编号 修改发票审核单状态
        invoiceCheckListMapper.updateStatusByCheckInvoiceId(checkInvoiceId, invoiceStatus);
    }

    //    根据发票审核单编号 修改打款申请审核状态
    @Override
    public void updatePayStatusByCheckInvoiceId(String checkInvoiceId, String payStatus) {
        invoiceCheckListMapper.updatePayStatusByCheckInvoiceId(checkInvoiceId, payStatus);
    }


}